package com.example.admin.controller;


import com.example.admin.common.Result;
import com.example.admin.entity.CommunityActivity;
import com.example.admin.service.ActivityService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin")
@CrossOrigin(origins = "http://localhost:8080")
public class ActivityController {


    @Autowired
    private ActivityService activityService;

    @GetMapping("/selectActivityPage")
    public Result<PageInfo> selectActivityPage(@RequestParam Integer pageNumber,@RequestParam Integer pageSize) {
        PageHelper.startPage(pageNumber,pageSize);
        List<CommunityActivity> communityActivityList=activityService.selectActivityPage();
        PageInfo<CommunityActivity> pageInfo=new PageInfo<>(communityActivityList);
        return Result.success(pageInfo);
    }

    @PostMapping("/addActivity")
    public Result addActivity(@RequestBody CommunityActivity communityActivity) {

       Result result= activityService.addActivity(communityActivity);
       return result;
    }

    @PutMapping("/editActivity")
    public Result editActivity(@RequestBody CommunityActivity communityActivity) {

      Result result=  activityService.editActivity(communityActivity);
      return result;
    }

    @DeleteMapping("/deleteActivityById/{activityId}")
    public Result deleteActivityById(@PathVariable Integer activityId) {
        Result result=activityService.deleteActivityById(activityId);

        return result;
    }

    @GetMapping("/selectActivityByName")
    public Result<PageInfo> selectActivityByName(@RequestParam String activityName) {

        PageHelper.startPage(1,10);
       List<CommunityActivity> communityActivityList= activityService.selectActivityByName(activityName);
       PageInfo<CommunityActivity> pageInfo=new PageInfo<>(communityActivityList);
       return Result.success(pageInfo);
    }



}
